package com.kehutong.crm.message.service;

import java.util.List;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.condition.ConditionLike.LikeType;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;
import com.kehutong.crm.message.entity.MessageClassifyCorp;
import com.kehutong.crm.message.entity.MessageCorp;
import com.kehutong.crm.message.entity.MessageUser;

@WebService("/crm/message/corp/service")
public class MessageCorpService {

    @Inject
    private JdbcSession jdbcSession;

    public Object list(JSONObject jsonObject) {
        FindPage<MessageCorp> findPage = jdbcSession.findPage(MessageCorp.class)
                                                .eq(jsonObject, "classifyId", "releaseStatus", "releaseUserName")
                                                .like(jsonObject, "title")
                                                .eq("deleted", false);

        String createTime = jsonObject.getString("createTime");
        if (Objects.nonEmpty(createTime)) {
            findPage.like("createTime", createTime, LikeType.RIGHT);
        }

        findPage.order("createTime", "desc")
                .page(jsonObject);

        return Result.success(findPage.exe());
    }

    public Object save(MessageCorp message) {
        setClassifyName(message);
        jdbcSession.insert(message);

        return Result.success();
    }


    public Object update(MessageCorp message) {
        setClassifyName(message);
        jdbcSession.updateById(message);

        return Result.success();
    }

    private void setClassifyName(MessageCorp message) {
        MessageClassifyCorp classify = Pool.get(MessageClassifyCorp.class, message.getClassifyId());
        if (Objects.nonNull(classify)) {
            message.setClassifyName(classify.getName());

            MessageCorp oldCommon = Pool.get(MessageCorp.class, message.getId());
            if (Objects.isNull(oldCommon)){
                int count = classify.getCount() + 1;
                classify.setCount(count);
                jdbcSession.updateById(classify);
            } else if (!message.getClassifyId().equals(oldCommon.getClassifyId())){
                int count = classify.getCount() + 1;
                classify.setCount(count);
                jdbcSession.updateById(classify);

                MessageClassifyCorp oldClassify = Pool.get(MessageClassifyCorp.class, oldCommon.getClassifyId());
                if (Objects.nonNull(oldClassify)) {
                    int oldCount = oldClassify.getCount() - 1;
                    oldClassify.setCount(oldCount);
                    jdbcSession.updateById(oldClassify);
                }
            } else {
                // continue
            }
        }
    }

    public Object delete(MessageCorp message) {
        if (Objects.isNull(message)) {
            return Result.error(0, "消息不存在！");
        }

        message.setDeleted(true);
        jdbcSession.updateById(message);

        MessageClassifyCorp classify = Pool.get(MessageClassifyCorp.class, message.getClassifyId());
        int count = classify.getCount() - 1;
        classify.setCount(count);
        jdbcSession.updateById(classify);

        List<MessageUser> messageUserList = jdbcSession.findArray(MessageUser.class)
                                                       .eq("msgId", message.getId())
                                                       .exe();
        messageUserList.forEach(e -> {
            e.setDeleted(true);
            jdbcSession.updateById(e);
        });

        return Result.success();
    }

}
